package com.heima.admin.interceptor;

import com.heima.utils.common.AdminThreadLoacalUtil;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Description TODO
 * @Author earl
 * @Data 2024/8/21下午2:38
 * @Version 1.0
 */
@Component
public class AdminInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String header = request.getHeader("userId");
        if(StringUtils.isEmpty(header)||header.equals(null)){
            return true;
        }
        AdminThreadLoacalUtil userThreadLocal = new AdminThreadLoacalUtil();
        userThreadLocal.setId(Integer.parseInt(header));
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        AdminThreadLoacalUtil.remove();
    }
}